﻿//说明文件没有找到，谁给删除了？！


using System;
using System.Data;
using System.Collections.Generic;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using OF.Component.Factory;


namespace OF.Component.BLL
{
    ///<summary>
    ///BaseLocation 业务逻辑类 
    ///</summary>
    public class BaseLocationOperate
    {
        private static readonly string CACHE_KEY = "OF.BLL.BaseLocationOperate";
        private static readonly IBaseLocation dal = BaseLocation.CreateInstance();


        ///<summary>
        ///根据ID取出信息
        ///</summary>
        public static BaseLocationInfo GetByID(int intID)
        {
            BaseLocationInfo info = (BaseLocationInfo)CacheUtility.GetCache(intID, CACHE_KEY);
            if (info == null)
            {
                info = dal.GetByID(intID);
                CacheUtility.AddCache(info, intID, CACHE_KEY);
            }
            return info;
        }

        ///<summary>
        ///插入
        ///</summary>
        public static int Insert(BaseLocationInfo info)
        {
            int reValue = dal.Insert(info);
            CacheUtility.RemoveCache(info.LocationID, CACHE_KEY);
            return reValue;
        }

        ///<summary>
        ///删除ByID
        ///</summary>
        public static void Delete(int intID)
        {
            dal.Delete(intID);
            CacheUtility.RemoveCache(intID, CACHE_KEY);
        }

        ///<summary>
        ///修改ByID
        ///</summary>
        public static void Update(BaseLocationInfo info)
        {
            dal.Update(info);
            CacheUtility.RemoveCache(info.LocationID, CACHE_KEY);
        }

        ///<summary>
        ///搜索
        ///</summary>
        public static IList<BaseLocationInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            object[] objList = (object[])CacheUtility.GetCache(CACHE_KEY, new string[] { pageIndex.ToString(), pageSize.ToString(), where, orderField.ToString(), isDesc.ToString() });
            if (objList == null)
            {
                objList = new object[2];
                objList[0] = dal.Search(out pageCount, pageIndex, pageSize, where, orderField, isDesc);
                objList[1] = pageCount;
                CacheUtility.AddCache(objList, CACHE_KEY, new string[] { pageIndex.ToString(), pageSize.ToString(), where, orderField.ToString(), isDesc.ToString() });
            }
            pageCount = Convert.ToInt32(objList[1]);
            return (IList<Model.BaseLocationInfo>)objList[0];
        }

        /// <summary>
        /// 根据省份ID获取城市信息
        /// </summary>
        /// <param name="Pid"></param>
        /// <returns></returns>
        public static DataTable GetLocation(int Pid)
        {
            DataTable dt = dal.GetLocation(Pid);
            return dt;
        }

        public static DataTable GetProvinceList()
        {
            return dal.GetProvinceList();
        }

        public static DataTable GetCityList(int provinceId)
        {
            return dal.GetCityList(provinceId);
        }

        public static DataTable GetAreaList(int cityId)
        {
            return dal.GetAreaList(cityId);
        }

        public static DataTable SearchCities(out int recordCount, int provinceId, int CityId, int pageIndex, int pageSize,bool addDefaultCity=false)
        {

            DataTable tb = dal.SearchCities(out recordCount, provinceId, CityId, pageIndex, pageSize);
            if (addDefaultCity)
            {
                DataRow row = tb.NewRow();
                row["ProvinceId"] = -1;
                row["ProvinceName"] = "";
                row["CityId"] = -1;
                row["CityName"] = "默认城市";
                tb.Rows.InsertAt(row,0);
            }
            return tb;
        }

        /// <summary>
        /// 根据省份名称，城市名称,区县名称搜索地址信息
        /// </summary>
        /// <param name="provinceName">省份名称</param>
        /// <param name="cityName">市名称</param>
        /// <param name="districtName">区县名称</param>
        /// <returns></returns>
        public static DataTable GetAddressInfo(string provinceName, string cityName,string districtName)
        {
            return dal.GetAddressInfo(provinceName, cityName,districtName);
        }

        public static IList<NodeCity> GetAllSecondCities()
        {
            return dal.GetAllSecondCities();
        }
    }
}